<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             v-loading="subloading"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="90%">
    <el-form :model="submitData"
             label-position="right"
             :rules='formModelRules'
             ref="formModelRef"
             label-width="150px">
      <el-form-item label="项目及意义：" prop="projectName">
        <el-input v-model="submitData.projectName"
                  clearable
                  style="width: 380px"
                  placeholder="项目及意义，不可重复"></el-input>
      </el-form-item>
      <el-form-item label="管理目标：" prop="targetManagement">
        <el-input v-model="submitData.targetManagement"
                  clearable
                  type="textarea"
                  style="width: 380px"
                  placeholder="管理目标"></el-input>
      </el-form-item>
      <el-form-item label="针对主要风险：" prop="healthRisks">
        <el-input v-model="submitData.healthRisks"
                  clearable
                  type="textarea"
                  autosize
                  style="width: 380px"
                  placeholder="针对主要风险"></el-input>
      </el-form-item>


      <el-form-item label="相关知识：">
        <vue-neditor-wrap :destroy="true"
                          v-model="submitData.medicalInterpretation"
                          :config="myConfig"></vue-neditor-wrap>
      </el-form-item>
      <el-form-item label="前端排序：" prop="sortNum">
        <el-input v-model="submitData.sortNum"
                  clearable
                  style="width: 380px"
                  placeholder="前端排序"></el-input>
      </el-form-item>

      <el-form-item label="判断标准配置：" prop="noDataControlDay">
        <div style="display: flex;flex-direction: row">
          <p class="tv1">连续</p>
          <el-input v-model="submitData.noDataControlDay"
                    clearable
                    style="width: 80px;margin-left: 10px;margin-right: 10px"
                    placeholder="必填"></el-input>
          <p class="tv1">天无数据</p>
        </div>
      </el-form-item>
      <el-form-item label="项目当前状态选项：" class="is-required">
        <el-input v-model="submitData.tag"
                  clearable
                  style="width: 180px;"
                  placeholder="请输入状态"></el-input>
        <el-select v-model="submitData.status"
                   clearable
                   class="w-200"
                   placeholder="请选择结论">
          <el-option v-for="item in statusList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>

        <el-select v-model="submitData.diseasesTag"
                   clearable
                   filterable
                   multiple
                   value-key="id"
                   class="w-200"
                   placeholder="请选择病种">
          <el-option v-for="item in diseasesDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item">
          </el-option>
        </el-select>


        <el-button type="primary" @click="handleAddTag">增 加</el-button>
      </el-form-item>

      <el-form-item v-if="submitData.targetStates.length>0">
        <element-table :columns="columnsStatus"
                       :data="submitData.targetStates">

          <template v-slot:stateStatus="{row}">
            <el-tag :type="row.stateStatus==1?'success':'danger'">{{ row.stateRerult }}</el-tag>
          </template>
          <template v-slot:diseasesTag="{row}">
            <el-tag  v-for="(item,index) in row.stateDiseases" :key="index" type='success' style="margin-left: 12px">{{ item.diseaseName }}</el-tag>
          </template>
          <template v-slot:option="{row,index}">
            <el-button type="danger" :disabled="row.isOverRange==1"
                       @click="handlerDelTag(index)">删除
            </el-button>
          </template>
        </element-table>
      </el-form-item>

      <el-form-item label="数据来源：" class="is-required">
        <el-select v-model="submitData.sourceType"
                   clearable
                   class="w-200"
                   @change="handleDataFrom"
                   placeholder="请选择">
          <el-option v-for="item in beanStatusList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="项目对应字段：" prop="relatedIndex"  v-if="submitData.sourceType==2">
        <el-select v-model="submitData.relatedIndex"
                   clearable
                   class="w-200"
                   placeholder="请选择项目对应字段">
          <el-option v-for="item in zhibiaoList"
                     :key="item.id"
                     :label="item.name+(item.unit?item.unit:'')"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="填写内容：" class="is-required" v-if="submitData.sourceType==2">
        <el-select v-model="submitData.indexItemIds"
                   clearable
                   multiple
                   class="w-200"
                   placeholder="请选择">
          <el-option v-for="item in zhibiaoList"
                     :key="item.id"
                     :label="item.name+(item.unit?item.unit:'')"
                     :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="关联量表：" class="is-required" v-if="submitData.sourceType==3">
        <el-select v-model="submitData.scales"
                   clearable
                   multiple
                   filterable
                   style="width: 380px"
                   @change="handleChangeScale()"
                   placeholder="请选择">
          <el-option v-for="item in liangbiaoList"
                     :key="item.id"
                     :label="item.scaleName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型：" class="is-required" v-if="submitData.sourceType==4">
        <el-radio-group v-model="submitData.deviceType" @change="handleChangeType">
          <el-radio :label='1'>关联检测项</el-radio>
          <el-radio :label='2'>指标</el-radio>
        </el-radio-group>


      </el-form-item>

      <el-form-item class="is-required" label="关联检测项：" v-if="submitData.deviceType==1&&submitData.sourceType==4">
        <el-select v-model="detectIds"
                   value-key="name"
                   filterable
                   remote
                   clearable
                   reserve-keyword
                   placeholder="请输入检测项"
                   :remote-method="searchMethod"
                   :loading="loading"
                   @change="handleSelectItems"
                   class="w-200">
          <el-option v-for="item in optionsFilterList"
                     :key="item.id"
                     :label="item.name"
                     :value="item">
          </el-option>
        </el-select>
        <el-button type="primary"
                   @click="handleAddDetect(detectIds)">新 增
        </el-button>

      </el-form-item>

      <el-form-item v-if="detectList.length>0&&submitData.deviceType==1&&submitData.sourceType==4">
        <element-table :columns="columnsDetect"
                       style="width: 450px"
                       :data="detectList">
          <template v-slot:option="{row,index}">
            <el-button type="danger"
                       @click="handlerDelDetect(index)">删除
            </el-button>
          </template>
        </element-table>
      </el-form-item>


      <el-form-item label="指标：" v-if="submitData.deviceType==2&&submitData.sourceType==4">
        <el-select v-model="indexItem"
                   value-key="name"
                   filterable
                   remote
                   clearable
                   reserve-keyword
                   placeholder="请输入指标项"
                   :remote-method="searchMethodIndexItem"
                   :loading="loading"
                   @change="handleIndexItem"
                   class="w-200">
          <el-option v-for="item in searchIndexList"
                     :key="item.id"
                     :label="item.name"
                     :value="item">
          </el-option>
        </el-select>

        <el-button type="primary"
                   @click="handleAddIndex(indexItem)">新 增
        </el-button>


      </el-form-item>

      <el-form-item v-if="chooseIndexList.length>0&&submitData.deviceType==2&&submitData.sourceType==4">
        <element-table :columns="columnsIndex"
                       style="width: 450px"
                       :data="chooseIndexList">
          <template v-slot:option="{row,index}">
            <el-button type="danger"
                       @click="handlerDelIndex(index)">删除
            </el-button>
          </template>
        </element-table>
      </el-form-item>

      <el-form-item v-if="submitData.sourceType==5" label="题目:" prop="transformSubject"
                    :rules="submitData.sourceType==5?formModelRules.transformSubject:[{required: false}]">
        <el-input v-model="submitData.transformSubject"
                  clearable
                  style="width: 380px;"
                  placeholder="请输入题目"/>
        <el-button type="primary" @click="handleAddOption">增加选项</el-button>
      </el-form-item>


      <el-form-item v-for="(item,index) in submitData.transformOptions" :key="index+'img'"
                    v-if="submitData.sourceType==5">
        <el-upload :action="fileLogoUploadAction"
                   :max-size="fileMaxSize"
                   class="avatar-uploader"
                   :show-file-list="false"
                   :on-exceeded-size="showError"
                   :on-success="uploadLogoSuccess">
          <img v-if="item.imgUrl"
               :src="item.imgUrl"
               width="80px"
               height="80px"
               style="border-radius: 5%"
               class="avatar">
          <i v-else
             class="el-icon-plus avatar-uploader-icon" @click="handleChooseImg(index)"></i>
          <i v-if="item.imgUrl" @click.stop="handleDelImg(index)" class="del el-icon-error"></i>
        </el-upload>
        <div style="display: flex;flex-direction: row;align-items: end">

          <el-input v-model="item.content"
                    clearable
                    type="textarea"
                    style="width: 280px;margin-right: 10px"
                    placeholder="请输入选项"/>
          <el-input v-model="item.score"
                    clearable
                    style="width: 150px;margin-right: 10px"
                    placeholder="选项得分"/>
          <el-input v-model="item.sortNum"
                    type="number"
                    style="width: 150px;margin-right: 10px"
                    placeholder="前端排序"/>
          <el-button type="danger"
                     @click="handlerDelOption(index)">删除
          </el-button>
        </div>
      </el-form-item>
      <el-form-item label="备注:" v-if="submitData.sourceType==5">
        <el-input v-model="submitData.note"
                  clearable
                  type="textarea"
                  style="width: 380px"
                  placeholder="请输入备注"/>
      </el-form-item>


      <el-form-item label="判断标准配置：" class="is-required" v-if="submitData.sourceType!=1">
        <el-button type="primary" @click="handleAddTiaojian()">增 加</el-button>
      </el-form-item>

      <el-form-item v-for="(item,index) in submitData.conditions" :key="index+'a'" v-if="submitData.sourceType!=1">

        <div style="display: flex;flex-direction: row">
          <el-select v-model="item.scaleId"
                     clearable
                     v-if="submitData.sourceType==3"
                     style="width: 220px;margin-right: 5px"
                     placeholder="对应量表">
            <el-option v-for="item in selectScaleList"
                       :key="item.id"
                       :label="item.scaleName"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-select v-model="item.indexResultType"
                     v-if="submitData.sourceType==4"
                     style="width: 120px;margin-right: 5px"
                     placeholder="选择关联检测项类型">
            <el-option v-for="item in testTypeList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>


          <el-select v-model="item.genderType"
                     clearable
                     style="width: 120px"
                     placeholder="选择性别">
            <el-option v-for="item in sexList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-select v-model="item.relatedDiseaseId"
                     clearable
                     style="width: 120px;margin-right: 5px;margin-left: 5px"
                     placeholder="关联病种">
            <el-option v-for="item in bingzhongList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-select v-model="item.unRelatedDiseaseId"
                     clearable
                     style="width: 120px"
                     placeholder="未关联病种">
            <el-option v-for="item in bingzhongList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>

          <el-input v-model="item.minAge"
                    clearable
                    style="width: 100px;margin-right: 5px;margin-left: 5px"
                    placeholder="最小年龄"/>


          <el-input v-model="item.maxAge"
                    clearable
                    style="width: 100px;margin-right: 5px"
                    placeholder="最大年龄"/>

          <el-input v-model="item.firstValue"
                    clearable
                    placeholder="左边值"
                    v-if="item.indexResultType==1||submitData.sourceType!=4"
                    style="width: 100px;"/>
          <el-select v-model="item.firstCondition"
                     clearable
                     v-if="item.indexResultType==1||submitData.sourceType!=4"
                     style="width: 120px;margin-right: 5px;margin-left: 5px"
                     placeholder="关系">
            <el-option v-for="item in fuhaoLeft"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>

          <el-input style="width: 100px" disabled value="选项得分" v-if="submitData.sourceType==5"/>
          <el-input style="width: 100px" disabled value="量表评分" v-else-if="submitData.sourceType==3"/>

          <el-select v-model="item.indexItemId"
                     clearable
                     style="width: 220px"
                     placeholder="请选择"
                     v-else-if="submitData.sourceType==2">
            <el-option v-for="item in zhibiaoList"
                       :key="item.id"
                       :label="item.name+(item.unit?item.unit:'')"
                       :value="item.id">
            </el-option>
          </el-select>

          <el-select v-model="item.indexName"
                     clearable
                     style="width: 220px"
                     @click.native="handleChhooseIndexItem"
                     @change="handleChoonseIndex(index)"
                     placeholder="请选择"
                     v-else>
            <el-option v-for="item in indexItemList"
                       :key="item.name"
                       :label="item.name+(item.unit?item.unit:'')"
                       :value="item.name">
            </el-option>
          </el-select>

          <el-select v-model="item.secondCondition"
                     clearable
                     v-if="item.indexResultType==1||submitData.sourceType!=4"
                     style="width: 120px;margin-right: 5px;margin-left: 5px"
                     :disabled="item.firstCondition==1"
                     placeholder="关系">
            <el-option v-for="item in fuhaoRight"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-input v-model="item.secondValue"
                    clearable
                    v-if="item.indexResultType==1||submitData.sourceType!=4"
                    placeholder="右边值"
                    :disabled="item.firstCondition==1"
                    style="width: 100px;"/>
          <!--          定性选择-->
          <el-select v-model="item.firstValue"
                     clearable
                     v-if="submitData.sourceType==4&&item.indexResultType==2"
                     style="width: 220px;margin-left: 5px"
                     placeholder="请选择">
            <el-option v-for="item in qualitativeList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>

          <el-button type="danger" @click="handleDelTiaojian(index)" style="margin-left: 5px">删 除</el-button>
        </div>
      </el-form-item>
    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="submitForm()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>
import ElementTable from '@/base/ElementTable'
import VueNeditorWrap from "vue-neditor-wrap";

export default {
  name: "targetEditDialog",
  data() {
    return {
      title: '新 增',
      myConfig: {
        // 你的UEditor资源存放的路径,相对于打包后的index.html
        UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: '418',
        // 初始容器宽度
        initialFrameWidth: '100%',
        // 关闭自动保存
        enableAutoSave: false,
        scaleEnabled: true
      },
      submitData: {
        deviceType: '',
        devices: [],
        references: [],
        tag: '',
        status: '',
        diseasesTag: [],
        sex: 1,
        id: '',
        diseaseId: '',
        projectName: '',//项目名称
        targetManagement: '',//管理目标
        healthMeaning: '',//健康意义
        healthRisks: '',//健康风险
        sortNum: '',//前端排序
        noDataControlDay: '',//管控天数
        scales: [],//关联量表id
        indexItemIds: [],//手动填写的指标id
        conditions: [],//条件
        sourceType: 1,//数据来源类型
        targetStates: [],//添加的内容
        targetStates1: [],//添加的内容
        transformOptions: [],//用户选择转换
        transformSubject: '',
        medicalInterpretation: '',
        relatedIndex: '',
      },
      formModelRules: {
        projectName: [{required: true, message: '请输入项目及意义', trigger: 'blur'}],
        targetManagement: [{required: true, message: '请输入管理目标', trigger: 'blur'}],
        healthRisks: [{required: true, message: '请输入风险', trigger: 'blur'}],
        sortNum: [{required: true, message: '请输入排序', trigger: 'blur'}],
        noDataControlDay: [{required: true, message: '请输入判断标准配置', trigger: 'blur'}],
        transformSubject: [{required: true, message: '请输入题目', trigger: 'blur'}],
        relatedIndex: [{required: true, message: '请选择项目对应字段', trigger: 'blur'}]
      },
      statusList: [
        {
          name: '正常',
          id: '1',
        },
        {
          name: '异常',
          id: '0',
        }],
      beanStatusList: [
        {
          name: '手动选择',
          id: 1
        }, {
          name: '手动填写',
          id: 2
        }, {
          name: '量表',
          id: 3
        }, {
          name: '天下医生设备',
          id: 4
        }],
      qualitativeList: [
        {
          name: '阴性',
          id: 1
        }, {
          name: '弱阴性',
          id: 2
        }, {
          name: '弱阳性',
          id: 3
        }, {
          name: '阳性',
          id: 4
        }
      ],
      testTypeList: [{name: '定量', id: 1}, {name: '定性', id: 2}],
      zhibiaoList: [],
      liangbiaoList: [],
      diseasesDataSource: [],
      ids: [],
      type: '',
      detectIds: '',
      indexItem: '',
      indexContent: '',
      chooseIndexList: [],
      detect: '',
      loading: false,
      fuhaoRight: [
        {
          name: '<',
          id: 4
        }, {
          name: '<=',
          id: 5
        }
      ],
      fuhaoLeft: [
        {
          name: '=',
          id: 1
        }, {
          name: '<',
          id: 4
        }, {
          name: '<=',
          id: 5
        }],
      columnsStatus: [
        {
          label: '状态',
          prop: 'stateName',
          align: 'center',
        },
        {
          label: '结论',
          prop: 'stateStatus',
          align: 'center',
          isSlot: true
        },
        {
          label: '病种',
          prop: 'diseasesTag',
          align: 'center',
          isSlot: true
        },
        {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ],
      optionsFilterList: [],
      searchIndexList: [],
      indexItemList: [],
      detectList: [],
      columnsDetect: [
        {
          label: '检测项',
          prop: 'name',
          align: 'center',
        },
        {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ],
      sexList: [
        {
          name: '男女一致',
          id: 1
        }, {
          name: '男',
          id: 2
        }, {
          name: '女',
          id: 3
        }],
      satisfyList: [
        {
          name: '全部满足',
          id: 1
        }, {
          name: '任何一满足',
          id: 2
        }, {
          name: '满足任何两个',
          id: 3
        }],
      columnsIndex: [
        {
          label: '指标项',
          prop: 'name',
          align: 'center',
        },
        {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
        }
      ],
      bingzhongList: [],
      resultData: {},//接受传过来的参数
      fileLogoUploadAction: this.api.file.upload,
      fileMaxSize: 1024 * 50,
      imgIndex: 0,
      newData: {},
      subloading: false,
      selectScaleList: []
    }
  },
  props: {
    eventdata: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.resultData = this.eventdata
    this.getHealthMapDiseaseList()
    this.getHealthMapIndexItemList()
    this.getScaleAll()
    this.getAllDiseaseList()
    if (this.resultData.childId) {
      this.title = '编 辑'
      this.submitData.id = this.resultData.childId
      this.getHealthMapTargetDetail()
    } else {
      this.title = '新 增'
    }

  },
  components: {
    'element-table': ElementTable,
    VueNeditorWrap
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    //查询所有病种
    getAllDiseaseList() {
      this.post(this.api.healthMap.getAllDiseaseList).then((res) => {
        if (res.data.code === '200') {
          this.diseasesDataSource = res.data.object
        }
      })
    },
    //增加内容
    handleAddTag() {
      let list = this.submitData.targetStates
      if (this.submitData.tag && this.submitData.status) {
        let stateDiseases = this.submitData.diseasesTag.map(o => ({
          diseaseId: o.id,
          diseaseName: o.name,
        }))
        list.push({
          stateName: this.submitData.tag,
          stateStatus: this.submitData.status,
          stateRerult: this.statusList.filter(item => item.id == this.submitData.status)[0].name,
          stateDiseases: stateDiseases
        })
        this.submitData.tag = ''
        this.submitData.status = ''
        this.submitData.diseasesTag = []
      } else {
        this.$message.error('请输入状态和结论')
      }
    },
    //删除内容
    handlerDelTag(index) {
      this.submitData.targetStates.splice(index, 1)
    },
    //选择量表
    handleChangeScale() {
      let scales = this.submitData.scales
      let liangbiaoList = this.liangbiaoList;
      this.selectScaleList = []
      liangbiaoList.forEach(item => {
        scales.forEach(its => {
          if (item.id === its) {
            this.selectScaleList.push(item)
          }
        })
      })

      let copyScaleList = JSON.parse(JSON.stringify(this.selectScaleList))
      copyScaleList = copyScaleList.map(m => m.id)

      let conditions = this.submitData.conditions;
      conditions.forEach(c => {
        if (copyScaleList.indexOf(c.scaleId) < 0) {
          c.scaleId = ''
        }
      })
    },
    //切换数据来源
    handleDataFrom() {
      this.indexItemList = []
      this.submitData.conditions.forEach(item => {
        item.indexItemId = ''
      })
    },
    handleChangeType() {
      this.indexItemList = []
      this.submitData.conditions.forEach(item => {
        item.indexItemId = ''
        item.indexName = ''
      })
    },
    // 搜索联检测项
    searchMethod(query) {
      if (query !== '') {
        let _this = this;
        _this.loading = true; // 锁住
        this.post(this.api.promote.getDeviceList, {
          name: query,
          pageIndex: 0,
          pageSize: 10
        }).then(res => {
          if (res.data.code === '200') {
            _this.loading = false;
            _this.optionsFilterList = res.data.object;
          }
        })

      } else {
        this.optionsFilterList = [];
      }
    },
    //搜索指标项
    searchMethodIndexItem(query) {
      if (query !== '') {
        let _this = this;
        _this.loading = true; // 锁住
        this.post(this.api.healthMap.getIndexList, {
          name: query,
          pageIndex: 0,
          pageSize: 10
        }).then(res => {
          if (res.data.code === '200') {
            _this.loading = false;
            _this.searchIndexList = res.data.object;
          }
        })

      } else {
        this.searchIndexList = [];
      }
    },
    //选中指标项
    handleIndexItem(item) {
      this.indexContent = item
    },
    // 选中关联检测项
    handleSelectItems(item) {
      this.detect = item
    },
    handleAddIndex(indexItem) {
      if (indexItem) {
        if (this.chooseIndexList.some(item => item.id == indexItem.id)) {
          this.$message.warning('请勿重复添加!');
        } else {
          this.chooseIndexList.push(this.indexContent)
        }

      } else {
        this.$message.error('请选择指标项!');
      }
    },
    //点击获取数据源
    handleChhooseIndexItem() {
      if (this.submitData.sourceType == 2) {
        this.indexItemList = this.zhibiaoList
      } else {
        if (this.submitData.deviceType == 1 && this.detectList.length > 0) {
          //检测项ID查询指标
          this.ids = this.detectList.map(item => item.id)
          this.getDetectionIndexReferencesByDeviceId()
        } else if (this.submitData.deviceType == 2 && this.chooseIndexList.length > 0) {
          //直接用已选指标
          this.indexItemList = this.chooseIndexList
        } else {
          this.$message.error('请选择关联项或指标项')
        }
      }

    },
    //选择指标项
    handleChoonseIndex(index) {
      let conditions = this.submitData.conditions;
      let indexItemList = this.indexItemList;
      let filterData = indexItemList.find(item => item.name == conditions[index].indexName);
      conditions[index].indexItemId = filterData.id
      if (filterData.type) {
        conditions[index].indexItemType = filterData.type
      } else {
        conditions[index].indexItemType = 1
      }
      this.submitData.conditions = conditions
    },
    handleAddDetect(detectIds) {
      if (detectIds) {
        if (this.detectList.some(item => item.id == detectIds.id)) {
          this.$message.warning('请勿重复添加!');
        } else {
          this.detectList.push(this.detect)
        }

      } else {
        this.$message.error('请选择关联检测项!');
      }
    },
    //增加条件
    handleAddTiaojian() {
      var list = this.submitData.conditions
      let selectScaleList = this.selectScaleList;
      let scaleId = ''
      if (selectScaleList.length === 1) {
        scaleId = selectScaleList[0].id
      }
      var data = {
        genderType: '',
        relatedDiseaseId: '',
        unRelatedDiseaseId: '',
        minAge: '',
        maxAge: '',
        indexItemId: '',
        indexName: '',
        indexItemType: 1,
        firstCondition: '',
        firstValue: '',
        secondCondition: '',
        secondValue: '',
        scaleId: scaleId,
        indexResultType: 1,
      }
      list.push(data)
    },
    //删除条件
    handleDelTiaojian(index) {
      this.submitData.conditions.splice(index, 1)
    },
    handlerDelDetect(index) {
      this.detectList.splice(index, 1)
    },
    handlerDelIndex(index) {
      this.chooseIndexList.splice(index, 1)
    },
    //提交
    submitForm() {
      this.$refs.formModelRef.validate(async (valid) => {
        if (valid) {

          this.newData = JSON.parse(JSON.stringify(this.submitData))

          this.newData.devices = this.detectList
          this.newData.references = this.chooseIndexList
          this.newData.scales = this.newData.scales.map(item => {
            return {
              id: item
            }
          })
          //定性，左边符号固定为等于，左边值为阴阳性
          this.newData.conditions.forEach(item => {
            if (item.indexResultType == 2) {
              item.firstCondition = 1
              item.secondCondition = ''
              item.secondValue = ''
            }

          })
          this.newData.relatedIndex={id:this.newData.relatedIndex}
          this.updateHealthMapTarget()
        }
      })

    },
    //新增指标
    updateHealthMapTarget() {
      this.subloading = true
      this.newData.diseaseId = this.resultData.diseaseId
      this.post(this.api.healthMap.updateHealthMapTarget, this.newData).then((res) => {
        this.subloading = false
        if (res.data.code === '200') {
          this.$message.success('操作成功')
          this.$parent.getHealthMapTargetList()
          this.modalClose()

        }
      })
    },
    //查询指标
    getHealthMapIndexItemList() {
      this.post(this.api.healthMap.getHealthMapIndexItemList).then((res) => {
        if (res.data.code === '200') {
          this.zhibiaoList = res.data.object

        }
      })
    },
    //查询量表
    getScaleAll() {
      this.post(this.api.healthMap.getScaleAll, {
        pageSize: 100
      }).then((res) => {
        if (res.data.code === '200') {
          this.liangbiaoList = res.data.object
        }
      })
    },
    //查询病种
    getHealthMapDiseaseList() {
      this.post(this.api.healthMap.getHealthMapDiseaseExcludeList, {
        id: this.resultData.diseaseId
      }).then((res) => {
        if (res.data.code === '200') {
          this.bingzhongList = res.data.object
        }
      })
    },
    //通过检测项查询指标
    getDetectionIndexReferencesByDeviceId() {
      this.post(this.api.healthMap.getDetectionIndexReferencesByDeviceId, {
        deviceIds: this.ids
      }).then((res) => {
        if (res.data.code === '200') {
          this.indexItemList = res.data.object
        }
      })
    },
    //查询详情
    getHealthMapTargetDetail() {
      this.post(this.api.healthMap.getHealthMapTargetDetail, {
        id: this.submitData.id
      }).then((res) => {
        if (res.data.code === '200') {
          let bean = res.data.object
          this.submitData=Object.assign(this.submitData,bean)


          if (bean.relatedIndex){
            this.submitData.relatedIndex=bean.relatedIndex.id
          }
          // if (bean.transformOptions) {
          //   this.submitData.transformOptions = bean.transformOptions
          // }

          if (bean.scales) {
            this.selectScaleList = bean.scales
            this.submitData.scales = bean.scales.map(item => item.id)
          }
          if (bean.indexItemIdStr) {
            this.submitData.indexItemIds = bean.indexItemIdStr.split(',').map(Number)
          }
          if (bean.targetStates) {
            this.submitData.targetStates = bean.targetStates.map(item => ({
              stateName: item.stateName,
              stateStatus: item.stateStatus,
              stateRerult: item.stateStatus == 1 ? '正常' : '异常',
              isOverRange: item.isOverRange ? item.isOverRange : 0,
              stateDiseases: item.stateDiseases ? item.stateDiseases :[],
            }))
            this.submitData.targetStates1 = bean.targetStates

          }
          this.submitData.conditions = bean.conditions ? bean.conditions : []
          if (this.submitData.conditions) {
            this.submitData.conditions.forEach((item, index) => {
              this.submitData.conditions[index].tiaojianName = '条件' + (index + 1)
            })
          }

          if (bean.devices) {
            this.submitData.devices = bean.devices
            this.detectList = bean.devices
          }
          if (bean.references) {
            this.submitData.references = bean.references
            this.chooseIndexList = bean.references
          }

          if (bean.deviceType == 1 && bean.sourceType == 4) {
            this.ids = this.detectList.map(item => item.id)
            this.getDetectionIndexReferencesByDeviceId()
          } else if (bean.deviceType == 2 && bean.sourceType == 4) {
            this.indexItemList = this.chooseIndexList
          }
        }
      })
    },
    //增加选项
    handleAddOption() {
      this.submitData.transformOptions.push({
        imgUrl: '',
        content: '',
        sortNum: '',
        score: ''
      })
    },
    handlerDelOption(index) {
      this.submitData.transformOptions.splice(index, 1)
    },
    showError(file, fileList) {
      this.$Notice.warning({
        title: '文件大小超限',
        desc: "文件  " + file.name + " 太大，上传文件大小不能超过100M."
      });
    },
    //上传成功
    uploadLogoSuccess(res) {
      if (res.code === '200') {
        this.$message.success('上传成功');
        // this.list[this.imgIndex].imgUrl = res.object[0]
        this.$set(this.submitData.transformOptions[this.imgIndex], 'imgUrl', res.object[0])
        // this.$previewRefresh();
      } else {
        this.$message.error('上传失败');
      }
    },
    //添加图片
    handleChooseImg(index) {
      this.imgIndex = index
    },
    //清除图片
    handleDelImg(index) {
      this.submitData.transformOptions[index].imgUrl = ''
    },
  }
}
</script>

<style scoped lang="scss">

.avatar-uploader {
  /deep/ .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .del {
      position: absolute;
      z-index: 99;
      top: 0;
      right: 0;
      font-size: 16px;
      color: #f00;
    }

    .avatar {
      width: 80px !important;
      height: 80px !important;
      display: block;
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 80px !important;
      height: 80px !important;
      line-height: 80px !important;
      text-align: center;
    }

    &.el-upload--picture-card {
      width: 80px !important;
      height: 80px !important;
      line-height: 80px !important;
    }

    &:hover {
      border-color: #409eff;
    }
  }

  /deep/ .el-upload-list--picture-card {
    .el-upload-list__item {
      width: 80px;
      height: 80px;
    }
  }
}

</style>
